package com.neusoft.neuiotms.dcom.service.change.impl;

import javax.persistence.EntityNotFoundException;
import javax.transaction.Transactional;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.neusoft.bizcore.activiti.service.ProcessInstanceService;
import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.file.service.FileService;
import com.neusoft.bizcore.web.constant.ExceptionConstants;
import com.neusoft.bizcore.web.exception.UnitedException;
import com.neusoft.neuiotms.dcom.converter.change.ChangePlanConverter;
import com.neusoft.neuiotms.dcom.dto.change.ChangePlanDTO;
import com.neusoft.neuiotms.dcom.model.change.ChangePlan;
import com.neusoft.neuiotms.dcom.repository.change.ChangePlanAttachmentRepository;
import com.neusoft.neuiotms.dcom.repository.change.ChangePlanRepository;
import com.neusoft.neuiotms.dcom.service.change.ChangePlanService;

@Service
public class ChangePlanServiceImpl implements ChangePlanService {

    @Autowired
    private ChangePlanRepository changePlanRepository;

    @Autowired
    private ChangePlanConverter changePlanConverter;

    @Autowired
    private ChangePlanAttachmentRepository changePlanAttachmentRepository;

    @Autowired
    private FileService fileService;

    @Autowired
    private ProcessInstanceService processInstanceService;

    @Override
    @Transactional
    public ChangePlanDTO create(final ChangePlanDTO dto, final UserBean user) {
        this.processInstanceService.setGlobalVariable(dto.getTaskId(), "significantChange",
                dto.getSignificantChange());

        // 查询当前workNum和TaskId下是否存在记录
        final ChangePlan oldModel = this.changePlanRepository.findByWorkOrderNumAndTaskId(
                dto.getWorkOrderNum(), dto.getTaskId());
        if (oldModel == null) { // 如果不存在新增
            dto.setId(null);
        }

        final ChangePlan model = this.changePlanConverter.toModel(dto);
        this.changePlanRepository.save(model);

        // 完成节点
        this.processInstanceService.complete(dto.getWorkOrderNum(), dto.getTaskId(), user);

        return this.changePlanConverter.toDTO(model);
    }

    @Override
    public ChangePlanDTO detail(final String workOrderNum, final String taskId, final UserBean user) {
        ChangePlan model = null;
        try {
            if (StringUtils.isNotBlank(workOrderNum) && !"NULL".equals(taskId)) {
                model = this.changePlanRepository.findByWorkOrderNumAndTaskId(workOrderNum, taskId);
                if (model == null) {
                    // 获取前一节点的TaskID
                    final String lastTaskId = this.processInstanceService.getLastTaskId(taskId, user);
                    if (lastTaskId == null) {
                        return new ChangePlanDTO();
                    } else {
                        model = this.changePlanRepository.findByWorkOrderNumAndTaskId(workOrderNum,
                                lastTaskId);
                    }
                }
            } else if ("NULL".equals(taskId)) {
                model = this.changePlanRepository.findByWorkOrderNum(workOrderNum);
            }
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND,
                    new Object[] {workOrderNum },
                    new Object[] {taskId });
        }
        return this.changePlanConverter.toDTO(model);
    }

    @Override
    public ChangePlanDTO findByWorkOrderNumAndTaskId(final String workOrderNum, final String taskId) {
        final ChangePlan model =
                this.changePlanRepository.findByWorkOrderNumAndTaskId(workOrderNum, taskId);
        return this.changePlanConverter.toDTO(model);
    }

    @Override
    @Transactional
    public void deleteMongoFile(final String fsId) {
        this.fileService.deleteByFsId(fsId);
        this.changePlanAttachmentRepository.deleteByFsId(fsId);
    }

    @Override
    public void deleteByWorkOrderNum(final String workOrderNum) {
        this.changePlanRepository.deleteByWorkOrderNum(workOrderNum);
    }

    @Override
    public ChangePlanDTO findByWorkNum(final String workOrderNum) {
        final ChangePlan model = this.changePlanRepository.findByWorkNum(workOrderNum);
        if (model != null) {
            return this.changePlanConverter.toDTO(model);
        } else {
            return null;
        }
    }

}
